package com.kyzh.core.pager.gamelist

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.tabs.TabLayoutMediator
import com.gushenge.core.beans.Server
import com.gushenge.core.impls.WealImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.adapters.ServerListAdapter
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.utils.startGameDetailActivity
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import kotlinx.android.synthetic.main.fragment_gameserver.*
import kotlinx.android.synthetic.main.recyclerview.*
import org.jetbrains.anko.toast

class GameServerFragment : BaseFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_gameserver, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {


        val titles = arrayListOf(getString(R.string.willOpenServer),getString(R.string.openServered))
        val adapter = Adapter(R.layout.recyclerview, titles)
        viewpager.adapter = adapter
        viewpager.currentItem = 0
        viewpager.offscreenPageLimit = 1
        TabLayoutMediator(tab,viewpager){ tab,position->
            tab.text = titles[position]

        }.attach()

    }


    private inner class Adapter(layout:Int,titles:MutableList<String>):BaseQuickAdapter<String,BaseViewHolder>(layout,titles){

        override fun convert(holder: BaseViewHolder, item: String) {
            val root = holder.getView<SmartRefreshLayout>(R.id.root)
            val recyclerView = holder.getView<RecyclerView>(R.id.recyclerView)
            val bean = mutableListOf<Server>()
            recyclerView.layoutManager = object : LinearLayoutManager(context){
                override fun canScrollHorizontally(): Boolean {
                    return false
                }
            }
            root.autoRefresh()
            val adapter = ServerListAdapter(R.layout.frag_home_notice_item,bean)
            recyclerView.adapter = adapter
            adapter.setOnItemClickListener { adapter, view, position ->
                context.startGameDetailActivity(bean[position].game_id)
            }
            var p1 =1
            var max1 = 1
            root.setOnLoadMoreListener {
                if (p1> max1){
                    context.toast(getString(R.string.noHaveMore))
                    root.finishLoadMore()
                }else{
                    WealImpl.getServer(holder.adapterPosition, p1, object : ResultListener {
                        override fun success(beans: Any, p: Int, max: Int) {
                            bean.addAll(beans as ArrayList<Server>)
                            adapter.notifyDataSetChanged()
                            p1 = p
                            max1 = max
                            root.finishLoadMore()
                        }
                    })
                }
            }
            root.setOnRefreshListener {
                bean.clear()
                WealImpl.getServer(holder.adapterPosition, 1, object : ResultListener {
                    override fun success(beans: Any, p: Int, max: Int) {
                        bean.addAll(beans as MutableList<Server>)
                        adapter.notifyDataSetChanged()
                        adapter.setEmptyView(R.layout.empty)
                        p1 = p
                        max1 = max
                        root.finishRefresh()
                    }
                })
            }
        }
    }

    override fun onResume() {
        super.onResume()
        root.autoRefresh()
    }
}